<template>
  <div class="body-container layout-padding">
    <el-form label-width="70px" style="margin-bottom: 10px">
      <el-row :gutter="5">
        <el-col :span="3">
          <el-form-item label="仓库名称">
            <el-select filterable clearable v-model="state.sd.cwhname" placeholder="仓库名称">
              <el-option  v-for="item in state.whList" :key="item.cwhcode" :label="item.cwhname" :value="item.cwhname" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item label="存货名称">
            <el-input v-model="state.sd.cinvname" placeholder="存货名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item label="规格型号">
            <el-input v-model="state.sd.cinvstd" placeholder="规格型号"/>
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-form-item label="现存量>0">
            <el-checkbox v-model="state.sd.iquantityflag" />
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-form-item label="打印量=0">
            <el-checkbox v-model="state.sd.barcodeflag" />
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-button size="default" type="primary" class="ml5" @click="searchCurrentStore">
            <el-icon><ele-Search /></el-icon>查询
          </el-button>
          <el-button size="default" type="danger" class="ml5" :loading="state.sstbLoading" @click="shuJuTongBu"  v-auths="[state.permissionType.sysAdmin, state.permissionType.cs_shujutongbu]"  >
            <el-icon><ele-Document /></el-icon>数据同步
          </el-button>
          <el-button size="default" type="primary" class="ml5" @click="createPrintHomePopup"  v-auths="[state.permissionType.sysAdmin, state.permissionType.cs_scancode]"  >
            <el-icon><ele-Printer /></el-icon>打印二维码
          </el-button>
          <el-button size="default" type="primary" class="ml5" @click="addPreparePrint"  v-auths="[state.permissionType.sysAdmin, state.permissionType.cs_scancode]"  >
            <el-icon><ele-Plus /></el-icon>添加备选
          </el-button>
          <el-button size="default" type="primary" class="ml5" @click="createPreparePrintHomePopup"  v-auths="[state.permissionType.sysAdmin, state.permissionType.cs_scancode]"  >
            <el-icon><ele-Printer /></el-icon>打印备选
          </el-button>
          <el-button size="default" type="primary" class="ml5" @click="clearPreparePrint"  v-auths="[state.permissionType.sysAdmin, state.permissionType.cs_scancode]"  >
            <el-icon><ele-Delete /></el-icon>清空备选
          </el-button>
        </el-col>
      </el-row>
    </el-form>

    <el-table :data="state.csObj.list" border style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column label="仓库名称" prop="cwhname"></el-table-column>
      <el-table-column label="存货编码" prop="cinvcode"></el-table-column>
      <el-table-column label="存货名称" prop="cinvname"></el-table-column>
      <el-table-column label="规格型号" prop="cinvstd"></el-table-column>
      <el-table-column label="批号" prop="cbatch" v-if="false"></el-table-column>
      <el-table-column label="单位名称" prop="ccomunitname"></el-table-column>
      <el-table-column label="现存数量" prop="iquantity"></el-table-column>
      <el-table-column label="打印数量" prop="barcodeflag"></el-table-column>
      <el-table-column label="操作" v-if="false">
        <template v-slot="scope">
          <el-button size="small" type="primary" @click="viewCs(scope.row.autoid)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="mt15" background layout="total,sizes, prev, pager, next"
                   @current-change="loadCurrentStore" @size-change="loadCurrentStore" :total="state.csObj.total"
                   v-model:current-page="state.sd.pageNum" v-model:page-size="state.sd.pageSize" :page-sizes="state.sd.pageSizes">
    </el-pagination>
    <el-dialog title="二维码打印" v-model="state.printForm.printTitleVisible" :close-on-click-modal="false" width="720px">
      <PrintHome :autoIds="state.selectedAutoIdArray" :typeId="state.printForm.typeId" :key="state.printForm.key"></PrintHome>
    </el-dialog>
    <el-dialog title="备选二维码打印" v-model="state.preparePrintForm.printTitleVisible" :close-on-click-modal="false" width="720px">
      <PrintHome :autoIds="state.prepareAutoIdArray" :typeId="state.preparePrintForm.typeId" :key="state.preparePrintForm.key"></PrintHome>
    </el-dialog>
  </div>
</template>

<script setup name="currentStoreIndex">
import request from '/@/utils/request';
import { ElMessage } from 'element-plus';
import mittBus from '/@/utils/mitt';
import other from '/@/utils/other';
import PermissionType from "/@/utils/PermissionType";
import {reactive, ref, onMounted, onUnmounted, defineAsyncComponent} from "vue";
import { useRouter } from "vue-router";
const router = useRouter();

const PrintHome = defineAsyncComponent(() => import('/@/views/kucun/cs/printhome.vue'));

const state = reactive({
  permissionType:PermissionType,
  sd: {
    pageNum: 1,
    pageSize: 100,
    pageSizes: [50, 100, 150, 200],
    cwhname:'',
    cinvcode:'',
    cinvname:'',
    cinvstd:'',
    iquantityflag:true,
    barcodeflag:false,
  },
  sstbLoading : false,
  csObj: {},
  whList : [],
  printForm:{
    printTitleVisible : false,
    title:'',
    key : '',
    typeId:1
  },
  selectedAutoIdArray : [],
  preparePrintForm:{
    printTitleVisible : false,
    title:'',
    key : '',
    typeId:1
  },
  prepareAutoIdArray : [],
})
const searchCurrentStore = ()=>{
  state.sd.pageNum = 1;
  loadCurrentStore();
}
const loadCurrentStore = ()=>{
  request({
    url: '/currentstore/pagecurrentstore',
    data: state.sd
  }).then(res => {
    const { data } = res
    state.csObj = data
  })
}
const loadWhList = ()=>{
  request({
    url: '/cangku/listuserwh',
    data: {}
  }).then(res => {
    const { data } = res
    state.whList = data
  })
}
const shuJuTongBu = ()=>{
  state.sstbLoading = true;
  request({
    url: '/currentstore/shujutongbu',
    timeout: 5000000,
    data: {}
  }).then(res => {
    state.sstbLoading = false;
    if(res.code !== "200"){
      ElMessage.error(res.msg)
    }else{
      ElMessage.success("插入" + res.data.insertcount + "条,修改" + res.data.updatecount + "条,异常" + res.data.exceptioncount + "条" );
    }
  })
}
/*打印相关*/
const createPrintHomePopup = ()=>{
  if(state.selectedAutoIdArray.length === 0){
    ElMessage.error("请勾选打印的物料")
    return
  }
  if(state.selectedAutoIdArray.length > 100){
    ElMessage.error("最多打印100条物料")
    return
  }
  state.printForm.printTitleVisible = true
  state.printForm.typeId=1
}
const createPrintHomePopup40 = ()=>{
  state.printForm.printTitleVisible = true
  state.printForm.typeId=2
}
const handleSelectionChange = (selectedRow)=>{
  state.selectedAutoIdArray = [];
  state.printForm.key = other.uuid();
  if(Array.isArray(selectedRow)){
    selectedRow.forEach((row)=>{
      state.selectedAutoIdArray.push(row.autoid)
    })
  }
}

/*备选打印*/
const addPreparePrint = ()=>{
  if(state.selectedAutoIdArray.length === 0){
    ElMessage.error("请勾选备选打印的物料")
    return
  }
  try {
    state.selectedAutoIdArray.forEach((item, index)=>{
      if(!state.prepareAutoIdArray.some(pItem => pItem == item)){
        if(state.prepareAutoIdArray.length > 100){
          throw new Error('error')
        }
        state.prepareAutoIdArray.push(item)
      }
    })
  }catch (e){
    ElMessage.error("最多备选100条物料")
    return
  }
  console.log(state.prepareAutoIdArray)
  ElMessage.success("操作成功")
  state.preparePrintForm.key = other.uuid();
}
const clearPreparePrint = ()=>{
  state.prepareAutoIdArray = []
  ElMessage.success("操作成功")
}
const createPreparePrintHomePopup = ()=>{
  if(state.prepareAutoIdArray.length === 0){
    ElMessage.error("备选打印二维码为0")
    return
  }
  if(state.prepareAutoIdArray.length > 100){
    ElMessage.error("备选打印二维码最多100条")
    return
  }
  state.preparePrintForm.printTitleVisible = true
  state.preparePrintForm.typeId=1
}

onMounted(()=>{
  loadWhList();
  loadCurrentStore();
})
onUnmounted(()=>{

})
</script>
<style scoped>
</style>
